<?php
namespace app\run\controller;

use think\Controller;
use think\Request;
use think\Db;

class Login extends Run
{
	public function login()
	{
		if (session('ADMIN_USER')) {
			$this->redirect('run/Index/index');
		} else {
			return view();

		}
	}
	public function checkLogin()
	{


		if (request()->isPost()) {
			
			$codeVal = input('post.code');
			$username = input('post.username');
			$pwd = trim(input('post.password'));
			// $data['password']= password_hash('admin',PASSWORD_DEFAULT);
			
			 
			$code = $this->check($codeVal);
			 
			
			if ($code) {
				$user = Db::name('admin_user')->where('username', $username)->find();
				if ($user) {
					if ($user['state'] != 1) {
						return 0;
					} else if (password_verify($pwd,$user['password'])) {

						session('ADMIN_USER', $user);
						return 1;
					} else {
						return 2;
					}
				} else {
					return 2;
				}

			} else {
				return 3;


			}
		}
	}
	public function logout()
	{
		session('ADMIN_USER', null);
		$this->redirect('Login/login');
	}

	private function check($code = '')
	{
		if (!captcha_check($code)) {
			return false;
		} else {
			return true;
		}
	}

}